﻿using System.Collections;

/// <summary>
/// 等待开始
/// </summary>
public class Game_WaitingState : GameState
{
	static Game_WaitingState _inst;

	public static Game_WaitingState inst {
		get {
			if (_inst == null) {
				_inst = new Game_WaitingState ();
			}
			return _inst;
		}
	}

	public override void Enter ()
	{
		base.Enter ();

		DebugInfo.Log ("Enter Game_WaitingState");

		if (GameDataKernel.inst.playing) {
			UIGameWindow.inst.HiddenAllBtns ();
		}
	}

	public override void Leave ()
	{
		DebugInfo.Log ("Leave Game_WaitingState");

		base.Leave ();
	}

	public override bool HandleMessage (BaseProceduar.Message message)
	{
		switch (message.msgId) {
		case Commands.CMD_CTRL_BUTTON_CLICKED:
			switch (message.iParam) {
			case GameCommands.CMD_DISSOLVE_ROOM:
				Hashtable reqArgs = new Hashtable ();
				reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
				reqArgs ["roomId"] = GameDataKernel.inst.roomId;
				UIRootController.inst.ShowMask (true);
				NetIOController.inst.SendMessage (
					new IOMessage (IOCmds.CMSG_PLAYER_REQ_DESTROY_ROOM, IOStates.STATE_OK, new HashtableEx (reqArgs)));
				return true;
			}
			break;
		}

		return base.HandleMessage (message);
	}

	public override bool ProcessPacket (IOMessage message)
	{
		switch (message.code) {
		case IOCmds.SMSG_PLAYER_JOIN_ROOM:
			if (message.state == IOStates.STATE_OK) {
				GameDataKernel.inst.HandleMessage (message);
			}
			return true;

		case IOCmds.SMSG_SET_PLAYER_READY:
			UIRootController.inst.ShowMask (false);
			if (message.state == IOStates.STATE_OK) {
				GameDataKernel.inst.HandleMessage (message);
			}
			return true;

		case IOCmds.SMSG_PLAYER_CANCEL_READY:
			UIRootController.inst.ShowMask (false);
			if (message.state == IOStates.STATE_OK) {
				GameDataKernel.inst.HandleMessage (message);
			}
			return true;

		case IOCmds.SMSG_SET_ROOM_INFO:
			if (message.state == IOStates.STATE_OK) {
				GameDataKernel.inst.HandleMessage (message);
				UIGameWindow.inst.HiddenAllBtns ();
				GameProcedure.inst.SwitchState (Game_GamingState.inst);
			}
			return true;

		case IOCmds.SMSG_SET_ROOM_FINISHED:
			UIRootController.inst.ShowMask (true);
			NetIOController.inst.isGame = false;
			NetIOController.inst.ConnectToNewURL (Constants.LOGIN_URL);
			return true;

		case IOCmds.SMSG_DEALER_CHANGED:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_ROUND_SETTEMENT:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.WS_CONN_OPEN:
				// 与服务器的连接打开
			UIRootController.inst.ShowMask (false);
			if (!NetIOController.inst.isGame) {
				if (_reconnecting) {
					UIGameReconnectWindow.CloseWindow ();
					GameController.inst.SwitchTo (GameProcedure.inst);
				} else {
					GameController.inst.SwitchTo (HomeProcedure.inst);
				}
			}
			return true;
		case IOCmds.WS_CONN_CLOSED:
			if (_reconnecting) {
				NetIOController.inst.Connect (null);
			} else {
				if (!NetIOController.inst.isGame) {
					// 正在重新连接到中央服务器
					UIRootController.inst.ShowMask (true);
					NetIOController.inst.Connect (null);
				} else {
					// 游戏服务器掉线，重新连接
					UIGameReconnectWindow.Popup ();
				}
			}
			return true;
		}
		return base.ProcessPacket (message);
	}
}
